package com.qst.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qst.common.annotation.Log;
import com.qst.common.core.controller.BaseController;
import com.qst.common.core.domain.AjaxResult;
import com.qst.common.enums.BusinessType;
import com.qst.system.domain.Message;
import com.qst.system.domain.vo.MessageVo;
import com.qst.system.service.IMessageService;
import com.qst.common.core.page.TableDataInfo;
import com.qst.common.utils.poi.ExcelUtil;
import com.qst.system.domain.Company;
import com.qst.system.service.ICompanyService;

/**
 * 留言Controller
 * 
 * @author linzhe
 * @date 2025-08-25
 */
@RestController
@RequestMapping("/system/message")
public class MessageController extends BaseController
{
    @Autowired
    private IMessageService messageService;
    
    @Autowired
    private ICompanyService companyService;

    /**
     * 查询留言列表
     */
    @PreAuthorize("@ss.hasPermi('system:message:list')")
    @GetMapping("/list")
    public TableDataInfo list(Message message)
    {
        startPage();
        List<MessageVo> list = messageService.selectMessageList(message);
        return getDataTable(list);
    }

    /**
     * 导出留言列表
     */
    @PreAuthorize("@ss.hasPermi('system:message:export')")
    @Log(title = "留言", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HttpServletResponse response, Message message)
    {
        List<MessageVo> list = messageService.selectMessageList(message);
        ExcelUtil<MessageVo> util = new ExcelUtil<MessageVo>(MessageVo.class);
        return util.exportExcel(list, "留言数据");
    }

    /**
     * 获取留言详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:message:query')")
    @GetMapping(value = "/{mId}")
    public AjaxResult getInfo(@PathVariable("mId") Long mId)
    {
        return AjaxResult.success(messageService.selectMessageById(mId));
    }

    /**
     * 查询公司名称列表
     */
    @PreAuthorize("@ss.hasPermi('system:message:list')")
    @GetMapping("/comName")
    public AjaxResult getCompanyName()
    {
        // 查询所有企业信息
        List<Company> companies = companyService.selectCompanyList(new Company());
        return AjaxResult.success(companies);
    }

    /**
     * 新增留言
     */
    @PreAuthorize("@ss.hasPermi('system:message:add')")
    @Log(title = "留言", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Message message)
    {
        return toAjax(messageService.insertMessage(message));
    }

    /**
     * 修改留言
     */
    @PreAuthorize("@ss.hasPermi('system:message:edit')")
    @Log(title = "留言", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Message message)
    {
        return toAjax(messageService.updateMessage(message));
    }

    /**
     * 删除留言
     */
    @PreAuthorize("@ss.hasPermi('system:message:remove')")
    @Log(title = "留言", businessType = BusinessType.DELETE)
    @DeleteMapping("/{mIds}")
    public AjaxResult remove(@PathVariable Long[] mIds)
    {
        return toAjax(messageService.deleteMessageByIds(mIds));
    }
}